from typing import Optional


class ListNode:
    def __init__(self, val=0, next_node=None):
        self.val = val
        self.next = next_node

# 创建链表 1 -> 2 -> 6 -> 3 -> 4 -> 5 -> 6
head = ListNode(1)
head.next = ListNode(2)
head.next.next = ListNode(6)
head.next.next.next = ListNode(3)
head.next.next.next.next = ListNode(4)
head.next.next.next.next.next = ListNode(5)
head.next.next.next.next.next.next = ListNode(6)

def print_linked_list(head: Optional[ListNode]):
    current = head
    while current:
        print(current.val, end=" -> ")
        current = current.next
    print("end")



class Test:
    def remove(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
        # 当前节点拼接一个头节点
        headNode = ListNode(0, head)
        node = headNode
        # 操作新链表
        while node.next:
            if (node.next.val == val):
                node.next = node.next.next
            else:
                node = node.next
        return headNode.next

if __name__ == '__main__':
    print_linked_list(head)
    # 测试方法
    test = Test()
    new_head = test.remove(head, 5)
    print_linked_list(new_head)
